package org.lk.controller;

import cn.hutool.db.meta.MetaUtil;
import cn.hutool.db.meta.TableType;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.lk.common.build.DataSourceBuild;
import org.lk.entity.DataSourceEntity;
import org.lk.mapper.DataSourceEntityMapper;
import org.lk.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.util.List;

/**
 * @author: lk
 * @since:
 */
@RestController
@RequestMapping("ds")
public class DsController {

    @Autowired
    private DataSourceEntityMapper dataSourceEntityMapper;


    @GetMapping("selectList")
    public ResponseResult selectList() {
        List<DataSourceEntity> dataSourceEntities = dataSourceEntityMapper.selectList(Wrappers.emptyWrapper());
        return ResponseResult.success(dataSourceEntities);
    }

    // 获取数据库中的表名称
    @GetMapping("getDsTableList")
    public ResponseResult getDsTableList(String id) {
        DataSourceEntity dataSourceEntity = dataSourceEntityMapper.selectById(id);
        DataSource dataSource = DataSourceBuild.build(dataSourceEntity);
        List<String> tables = MetaUtil.getTables(dataSource, TableType.TABLE);
        return ResponseResult.success(tables);
    }
}
